<?php

require_once("login/src/Model/LoginValidation.php");
require_once("Common/Model/CheckStrings.php");
require_once("MyWorkouts/Model/MyWorkout.php");
require_once("Database/Model/MyWorkoutDAL.php");
require_once("MyWorkouts/Model/MyWorkoutName.php");
require_once("MyWorkouts/Model/MyWorkoutDescription.php");

class WorkoutModel
{
	/**
	*@var UserInfoDAL
	**/
	private $userInfoDAL;
	/**
	*@var MyWorkoutDAL
	**/
	private $myWorkoutDAL;
	/**
	*@var LoginValidation
	**/
	private $loginValidation;

	/**
	*@var string session
	**/
	private static $errorMessage = "WorkoutModel::errorMessage";
	
	/**
	*@var string session
	**/
	private static $succesMessage = "WorkoutModel::succesMessage";


	public function __construct()
	{
		$this->userInfoDAL = new UserInfoDAL();
		$this->myWorkoutDAL = new MyWorkoutDAL();
		$this->loginValidation = new LoginValidation();
	}

	/**
	*@param string 
	*@param string
	*@return boolean
	**/
	public function validateWorkout($name, $description)
	{	$_SESSION[self::$errorMessage] = "";
	    $_SESSION[self::$succesMessage] = "";

		$bool = true;
		if(!$this->validateName($name))
		{
			$bool = false;
		}
		if(!$this->validateDescription($description))
		{
			$bool = false;
		}
		return $bool;
	}


	/**
	*@param string 
	*@return boolean
	**/
	private function validateName($name)
	{
		$bool = true;
		if(strlen($name) < MyWorkoutName::Min_Name_Length)
		{	 
			$bool = false;
			$_SESSION[self::$errorMessage] .= "   Du måste ange ett namn   .";
		}
		elseif(strlen($name) > MyWorkoutName::Max_Name_Length) 
		{ 
			$bool = false;
			$_SESSION[self::$errorMessage] .= "   Namnet är för långt, får max vara 40 tecken   .";
		}
		elseif(CheckStrings::hasTags($name))
		{ 
			$bool = false;
			$_SESSION[self::$errorMessage] .= "   Namnet innehåller ogiltiga tecken, tex < >   .";
		}
		 
		return $bool;
	}


	/**
	*@param string 
	*@return boolean
	**/
	private function validateDescription($description)
	{
		$bool = true;
		if(strlen($description) < MyWorkoutDescription::Min_Description_Length)
		{ 
			$bool = false;
			$_SESSION[self::$errorMessage] .= "   Du måste ange en beskrivning   .";
		}
		elseif(strlen($description) > MyWorkoutDescription::Max_Description_Length) 
		{ 
			$bool = false;
			$_SESSION[self::$errorMessage] .= "   Beskrivningen är för lång, får max 200tecken   .";
		}
		elseif(CheckStrings::hasTags($description))
		{ 
			$bool = false;
			$_SESSION[self::$errorMessage] .= "   Beskrivningen innehåller ogiltiga tecken, tex < >   .";
		}
		return $bool;
	}


	/**
	*@param string 
	*@param string
	*@param array
	**/
	public function CreateAndSaveWorkout($name, $description, $exercises)
	{
		$workout = MyWorkout::createNewWorkout($name, $description);
		$this->myWorkoutDAL->saveNewWorkout($workout, $exercises);
		$_SESSION[self::$succesMessage] = "Övningen har lagts till";
	}


	/**
	*@param int
	*@return boolean
	**/
	public function ValidateWorkoutIdAndDelete($workoutId)
	{
		$workout = $this->myWorkoutDAL->getWorkouts($workoutId); 
		$userID = $this->loginValidation->getLoggedInUserID();

		if($workout != null)
		{
			if($workout->compareUserID($userID))
			{
				$this->myWorkoutDAL->DeleteWorkout($workoutId);
				$_SESSION[self::$succesMessage] = "Övningen har tagits bort";
			}
		}
		$_SESSION[self::$errorMessage] .= "Ett fel uppstod när vi skulle ta bort tränings upplägget   .";
		return false;
	}

	/**
	*@return string session
	**/
	public function getErrorMessage()
	{
		if(isset($_SESSION[self::$errorMessage]))
		{
			return $_SESSION[self::$errorMessage]; 
		}
	}


	/**
	*@return string session
	**/
	public function getsuccesMessage()
	{
		if(isset($_SESSION[self::$succesMessage]))
		{
			return $_SESSION[self::$succesMessage]; 
		}
	}
}